package com.mrlee.controller;

import com.github.pagehelper.PageInfo;
import com.mrlee.bean.Account;
import com.mrlee.bean.Admin;
import com.mrlee.service.AccountService;
import com.mrlee.utils.AjaxResult;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by dllo on 18/3/13.
 */
@Controller
public class AccountController {

    @Resource
    private AccountService accountService;


    /**
     * 账务账号列表页
     * @return
     */
    @RequestMapping(value = "accountlist")
    public String accountList(){
        return "account/account_list";
    }

    /**
     * 账务账号详情页面
     * @return
     */
    @RequestMapping(value = "accountdetail")
    public String accountDetail(){
        return "account/account_detail";
    }

    /**
     * 账务账号添加页
     * @return
     */
    @RequestMapping(value = "accountadd")
    public String accountAdd(){
        return "account/account_add";
    }


    /**
     * 账务账号修改页
     * @return
     */
    @RequestMapping(value = "accountmodi")
    public String accountModi(){
        return "account/account_modi";
    }


    /**
     * 账务账号全部信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "allaccount")
    public AjaxResult selectAllAccount(){
        return accountService.selectAllAccount();
    }

    /**
     * 分页
     * @param pageNo 当前页
     * @param pageSize 每个页面显示的信息数量
     * @return
     */
    @RequestMapping(value = "accountpage")
    @ResponseBody
    public PageInfo<Account> adminByPage(@RequestParam(value = "pagenum",required = false)Integer pageNo,
                                       @RequestParam(value = "pagesize",required = false)Integer pageSize){
        return accountService.queryAccountByPage(pageNo,3);
    }

    /**
     * 更改状态
     * @param accountId  账务账号Id
     * @param status 账务账号状态
     * @return
     */
    @ResponseBody
    @RequestMapping("changestatus")
    public AjaxResult changeStatus(@RequestParam("accountId")Integer accountId,@RequestParam("status")Integer status){
        return accountService.changeStatus(accountId,status);
    }

    /**
     * 添加账务账号
     * @param name 真实姓名
     * @param cardid 身份证号
     * @param loginname 登录名
     * @param password 密码
     * @param password1 确认密码
     * @param tel 电话号
     * @param recommenderid 推荐人账号
     * @param email 邮箱
     * @param occupation 身份
     * @param gender 性别
     * @param address 地址
     * @param zipcode 邮政编码
     * @param qq qq号
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addaccount")
    public AjaxResult addAccount(@RequestParam("name")String name,
                                 @RequestParam("cardid")String cardid,
                                 @RequestParam("loginname")String loginname,
                                 @RequestParam("password")String password,
                                 @RequestParam("password1")String password1,
                                 @RequestParam("tel")String tel,
                                 @RequestParam("recommenderid")String recommenderid,
                                 @RequestParam("email")String email,
                                 @RequestParam("occupation")String occupation,
                                 @RequestParam("gender")String gender,
                                 @RequestParam("address")String address,
                                 @RequestParam("zipcode")String zipcode,
                                 @RequestParam("qq")String qq){

        AjaxResult ajaxResult = new AjaxResult();
        if ( "".equals(name)||"".equals(cardid)||"".equals(loginname)||"".equals(password)||"".equals(password1)||"".equals(tel)) {
            ajaxResult.setCode("0");
            ajaxResult.setMessage("请填写必填信息");
            return ajaxResult;
        }
        if (!password.equals(password1)){
            ajaxResult.setCode("0");
            ajaxResult.setMessage("密码不一致");
            return ajaxResult;
        }
        Account account = new Account();
        account.setRealName(name);
        account.setIdcardNo(cardid);
        account.setLoginName(loginname);
        account.setLoginPasswd(password);
        account.setTelephone(tel);
        account.setRecommenderId(recommenderid);
        account.setEmail(email);
        account.setOccupation(occupation);
        account.setGender(gender);
        account.setMailaddress(address);
        account.setZipcode(zipcode);
        account.setQq(qq);
        return accountService.addAccount(account);
    }

    /**
     * 查找当前账号信息
     * @param accountId 账务账号Id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectaccount")
    public AjaxResult selectAccount(@RequestParam("accountId")Integer accountId){
        return accountService.selectAccountById(accountId);
    }

    /**
     * 根据登录名查找账号
     * @param accountname 登录名
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectaccountbyaccountname")
    public AjaxResult selectAccountByAccountName(@RequestParam("accountname")String accountname){
        return accountService.selectAccountByAccountName(accountname);
    }


    /**
     * 根据身份证号查找账号
     * @param cardId 身份证号
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectaccountbycardid")
    public AjaxResult selectAccountByCardId(@RequestParam("cardId")String cardId){
        return accountService.selectAccountByCardId(cardId);
    }

    /**
     * 修改账号信息
     * @param accountId 账号Id
     * @param name 真实姓名
     * @param cardid 身份证号
     * @param loginname 登录账号
     * @param tel 电话
     * @param recommenderid 推荐人id
     * @param birthday 生日
     * @param email 邮箱
     * @param occupation  身份
     * @param gender 性别
     * @param address 地址
     * @param zipcode 邮政编码
     * @param qq qq号
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateaccount")
    public AjaxResult updateAccount(@RequestParam("accountId")Integer accountId,
                                    @RequestParam("name")String name,
                                    @RequestParam("cardid")String cardid,
                                    @RequestParam("loginname")String loginname,
                                    @RequestParam("tel")String tel,
                                    @RequestParam("recommenderid")String recommenderid,
                                    @RequestParam("birthday")String birthday,
                                    @RequestParam("email")String email,
                                    @RequestParam("occupation")String occupation,
                                    @RequestParam("gender")String gender,
                                    @RequestParam("address")String address,
                                    @RequestParam("zipcode")String zipcode,
                                    @RequestParam("qq")String qq){

        AjaxResult ajaxResult = new AjaxResult();
        if ( "".equals(name)||"".equals(cardid)||"".equals(loginname)||"".equals(tel)) {
            ajaxResult.setCode("0");
            ajaxResult.setMessage("请填写必填信息");
            return ajaxResult;
        }
        Account account = new Account();
        account.setAccountId(accountId);
        account.setRealName(name);
        account.setIdcardNo(cardid);
        account.setLoginName(loginname);
        account.setTelephone(tel);
        account.setRecommenderId(recommenderid);
        account.setBirthdate(birthday);
        account.setEmail(email);
        account.setOccupation(occupation);
        account.setGender(gender);
        account.setMailaddress(address);
        account.setZipcode(zipcode);
        account.setQq(qq);
        return accountService.updateAccount(account);
    }

    /**
     * 动态查找账号
     * @param name 姓名
     * @param cardid 身份证号
     * @param status 账号状态
     * @param loginname 登录名
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "searchaccount")
    public AjaxResult search(@RequestParam("name") String name,
                             @RequestParam("cardid") String cardid,
                             @RequestParam("status") String status,
                             @RequestParam("loginname") String loginname){
        return accountService.dynamicSearch(name,cardid,status,loginname);
    }

}
